<?php
namespace app\admin\controller;
use app\admin\model\Subject as modelSubject;
use app\admin\model\Question as modelQuestion;
use app\admin\model\Exam as modelExam;
use app\admin\model\Testing as modelTesting;
use think\Db;
class Testing extends Common
{
    function index(){
        return $this->fetch();
    }
    function ajaxTestingList(){
        $testingData = modelTesting::testingData();

        $this->assign('testingList',$testingData['list']);
        $this->assign('testingPage',$testingData['page']);
        $this->assign('testingCount',$testingData['count']);
        return $this->fetch('ajaxTestingList');
    }

    function testingAdd(){
        if(request()->isAjax()){
            //验证器
            (new \app\admin\validate\Testing()) -> goCheck();
            $data = modelTesting::disposeData(input('post.'),'add');
//            dump(input('post.'));
//            dump($data);exit;
            $res = modelTesting::create($data);
            if($res){
                exitjson(1,'保存成功');
            }else {
                exitjson(0, '保存失败');
            }
        }else{
            $examInfo = modelTesting::getExamInfo();

//            dump($examInfo);exit;
            $this->assign('examInfo',$examInfo);
            return $this->fetch('testingAdd');
        }
    }

    function testingEdit(){
        if(request()->isAjax()){
            //验证器
            (new \app\admin\validate\Testing()) -> goCheck();
            $data = modelTesting::disposeData(input('post.'));
            $testingid = input('testingid');

            $res = Db::name('testing')->where('testingid',$testingid)->update($data);
            if($res !== false){
                exitjson(1,'保存成功');
            }else {
                exitjson(0, '保存失败');
            }
        }else{
            $examInfo = modelTesting::getExamInfo();
            $one = modelTesting::getTesting(input('id'));
//            dump($examInfo);
//            dump($one);exit;
            $this->assign('examInfo',$examInfo);
            $this->assign('one',$one);
            $this->assign('testing_set',$one['testing_set']);
            return $this->fetch('testingEdit');
        }
    }

    function useTesting(){
        Db::name('testing')->where('is_active',1)->update(['is_active'=>0]); //去除默认
        $data['is_active'] = 1;
        $res = Db::name('testing')->where('testingid',input('id'))->update($data);
        if($res !== false){
            exitjson(1,'保存成功');
        }else{
            exitjson(0,'保存失败');
        }
    }

    function testingHide(){
        $testingid = input('id');
        if($testingid > 0){
            $testing = Db::name('testing')->find($testingid);
            if($testing['is_active']==1){
                exitjson(0,'当前使用中，不能删除');
            }
            $data['is_del'] = 1;
            $res = Db::name('testing')->where('testingid',$testingid)->update($data);
            if($res !== false){
                exitjson(1,'删除成功');
            }else{
                exitjson(0,'删除失败');
            }
        }
    }

}